package org.budo.elasticsearch.jdbc.driver.statement;

import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.budo.elasticsearch.jdbc.driver.BudoElasticsearchJdbcConnection;
import org.budo.elasticsearch.jdbc.driver.BudoElasticsearchJdbcPreparedStatement;
import org.budo.jdbc.driver.resultset.impl.ObjectArrayResultSet;
import org.elasticsearch.client.transport.TransportClient;

/**
 * @author li
 */
public class SHOW_DATABASES extends AbstractElasticsearchStatement {
    @Override
    public void execute(PreparedStatement preparedStatement) throws SQLException {
        BudoElasticsearchJdbcConnection connection = (BudoElasticsearchJdbcConnection) preparedStatement.getConnection();
        TransportClient transportClient = connection.getTransportClient();

        String[] indices = transportClient.admin().indices().prepareGetIndex().get().indices();

        // build resultSet
        ObjectArrayResultSet resultSet = new ObjectArrayResultSet(new String[] { "Database" },indices);

        ((BudoElasticsearchJdbcPreparedStatement) preparedStatement).setResultSet(resultSet);
    }
}
